package com.chefu.common.net.file.download;

import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * Created by ${wangshuyun} on 2016/10/18 0018.
 * <p>
 * <p/>
 * 文件下载
 */
public class DownloadManager {

    private static DownloadManager manager;
//    private DefaultApiService apiService = null;

    /***
     * 下载call对象
     */
    private Call<ResponseBody> responseBodyCall;

    public static DownloadManager getManager() {
        if (manager != null)
            return manager;

        manager = new DownloadManager();
        return manager;
    }

    private DownloadManager() {
        initApiService();
    }

    /**
     * 获取ApiService 对象
     *
     * @return
     */
    private void initApiService() {
//        final String BASE_URL = Latte.getConfiguration(ConfigKeys.API_HOST);
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(BASE_URL)
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .client(OkHttp.getFileOkHttpClient())
//                .build();
//        apiService = retrofit.create((Class<DefaultApiService>) Latte.getConfiguration(ConfigKeys.API_SERVICE));
    }

    /**
     * 下载文件
     *
     * @param filePath     文件地址
     * @param fileCallback
     */
//    public void downloadFile(@NonNull String filePath, @NonNull FileCallback fileCallback) {
//        if (apiService == null) {
//            initApiService();
//        } else {
//            this.responseBodyCall = apiService.downloadFile(filePath);
//            this.responseBodyCall.enqueue(fileCallback);
//        }
//
//    }

    /**
     * 取消下载
     */
    public void cancelDownload() {
        if (responseBodyCall != null && responseBodyCall.isCanceled() == false) {
            responseBodyCall.cancel();
        }
    }


}
